﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CanDichVu.Db;
using CanDichVu.Service.Iservice;

namespace CanDichVu.Service.Vervices
{
    public class UserSer : IUser
    {
        private readonly EntitiesCanDichVu dbContext = new EntitiesCanDichVu();

        public ThanhVien GetByUserName(string userName)
        {
            try
            {
                return dbContext.ThanhViens.Where(p => p.UserName == userName).First();
            }
            catch { return null; }
        }

        public ThanhVien GetByUserByEmail(string Email)
        {
            try
            {
                return dbContext.ThanhViens.Where(p => p.Email == Email).First();
            }
            catch { return null; }
        }

        public ThanhVien GetByUserById(int id)
        {
            try
            {
                return dbContext.ThanhViens.Where(p => p.Id == id).First();
            }
            catch { return null; }
        }

        public int addOrupdate(ThanhVien entity)
        {
            if (entity.Id == 0) dbContext.ThanhViens.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.Id;
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            try
            {
                var entityUser = dbContext.ThanhViens.First(k => k.UserName == username && k.Pass == oldPassword);
                entityUser.Pass = newPassword;
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }

       
        public bool deleteByName(string Name)
        {
            try
            {
                var entity = GetByUserName(Name);
                var listUserOnRoles = dbContext.UserInRoles.Where(p => p.Id == entity.Id).ToList();
                if (listUserOnRoles != null && listUserOnRoles.Count() > 0)
                {
                    foreach (var item in listUserOnRoles)
                    {
                        dbContext.UserInRoles.Remove(item);
                        dbContext.SaveChanges();
                    }
                }
                dbContext.ThanhViens.Remove(entity);
                dbContext.SaveChanges();

                return true;
            }
            catch { return false; }
        }


        public bool ValidateUser(string username, string password)
        {
            try
            {
                var entityUser = dbContext.ThanhViens.SingleOrDefault(k => k.UserName == username && k.Pass == password);
                if (entityUser != null)
                    return true;
                else return false;
            }
            catch { return true; }
        }

        public bool LoginUser(string Info, string password)
        {
            try
            {
                var entityUser = dbContext.ThanhViens.SingleOrDefault(k => (k.UserName == Info || k.Email == Info) && k.Pass == password);
                if (entityUser != null)
                    return true;
                else return false;
            }
            catch { return true; }
        }


        public ThanhVien LogIn(string username, string Password)
        {
            try
            {
                var entityUser = dbContext.ThanhViens.SingleOrDefault(k => k.UserName == username && k.Pass == Password);
                if (entityUser != null)
                    return entityUser;
                else return null;
            }
            catch { return null; }
        }

        public string[] GetRolesForUser(string username)
        {
            try
            {
                var c = (from i in dbContext.ThanhViens
                         join k in dbContext.UserInRoles on i.Id equals k.Id
                         join l in dbContext.Roles on k.RolesId equals l.Id
                         where i.UserName == username
                         select l.Name).ToArray();
                return c;
            }
            catch { return new string[0]; }
        }

        public bool AddUsersToRoles(int userId, string roleNames)
        {
            try
            {
                var roles = dbContext.Roles.FirstOrDefault(p => p.Name == roleNames);
                UserInRole uir = new UserInRole();
                uir.Id = userId;
                uir.RolesId = roles.Id;
                uir.UiRId = 0;
                dbContext.UserInRoles.Add(uir);
                dbContext.SaveChanges();
                return true;

            }
            catch { return false; }
        }


    }
}
